/**
 *
 */
package com.ozq.common.util;

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <公共工具>
 * <p>
 * <功能详细描述>
 * 
 * @author 欧增奇
 * @version [版本号, 2017年8月31日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class CommonUtils
{
    static Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    /**
     * 模糊查询新实现函数
     */
    public static final String FIND_IN_SET = "FIND_IN_SET";

    /**
     * 过滤文本中的script
     * 
     * @param textFilter
     *            待过滤Script的字符串
     * @return 过滤完成后的字符串
     */
    public static String filterScript(String textFilter)
    {
        if (StringUtils.isBlank(textFilter))
            return textFilter;

        return textFilter.replaceAll("\r|\n|\t", "&nbsp;").replaceAll("<(script.*)>(.*)<(\\/script)>", "&lt;$1&gt;$2&lt;$3&gt;");
    }

    /**
     * 条件拼接
     * 
     * @param whereMap
     *            条件map（支持值为String、Integer）
     * @param isWhere
     *            是否加入sql里面加入where
     *            <p>
     *            true:加入where,并首个参数不加入and
     *            <p>
     *            false:不加入where,并首个参数加入and
     * @param selectSQL
     *            当前的sql
     * @return
     */
    public static String getSelectSQLWhere(Map<String, Object> whereMap, boolean isWhere, String selectSQL)
    {

        boolean isFirst = true;
        String tempSql = "";
        for (Entry<String, Object> obj : whereMap.entrySet())
        {
            Object basic = obj.getValue();

            // 字符串
            if (basic instanceof String && StringUtils.isNotBlank(String.valueOf(basic)))
            {
                if (StringUtils.startsWith(obj.getKey(), FIND_IN_SET))
                    tempSql += (isWhere && isFirst ? " " : " and ") + "FIND_IN_SET('" + basic + "'," + obj.getKey().substring(FIND_IN_SET.length()) + ")";
                else
                    tempSql += (isWhere && isFirst ? " " : " and ") + obj.getKey() + " like '" + basic + "%'";
                isFirst = false;
            }

            // int
            if (basic instanceof Integer && basic != null)
            {
                tempSql += (isWhere && isFirst ? " " : " and ") + obj.getKey() + " = " + basic;
                isFirst = false;
            }

            // 时间
            if (basic instanceof Date && basic != null)
            {
                tempSql += (isWhere && isFirst ? " " : " and ") + obj.getKey() + " >= '" + TimeUtils.toStringTime((Date) basic, "yyyy-MM-dd 00:00:00") + "'";
                tempSql += " and " + obj.getKey() + " <= '" + TimeUtils.toStringTime((Date) basic, "yyyy-MM-dd 23:59:59") + "'";
                isFirst = false;
            }

            // 其他的类型直接忽悠
        }

        if (isWhere && tempSql.length() > 0)
        {
            selectSQL += " where " + tempSql;
        } else
        {
            selectSQL += tempSql;
        }
        return selectSQL;
    }
}
